// pages/guidePage/index.js
const app = getApp()
const utils = require('../../utils/util.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight: app.globalData.navHeight,
    tagTitles: [],
    tagsList: [],
    isChoose: false,
    screenHeight:wx.getSystemInfoSync().screenHeight
  },
  async init() {
    const recommendedList = []
    const styleList = await  utils.$http(app.globalData.api.GetStyleList, {}, 'GET', true, 'JSON').then(res => {
     return res && res.data
    })
    const specialTopicsList = await utils.$http(app.globalData.api.GetSpecialTopics, {}, 'GET', true, 'JSON').then(res => {
      return res && res.data
    })
    const familyList = await utils.$http(app.globalData.api.GetFamilyNumbers, {}, 'GET', true, 'JSON').then(res => {
     return res && res.data
    })
    recommendedList.push({
       recommendedTitel: '风格关注',
       recommendedCode: 'TJ001',
       rkv: styleList
     })
     recommendedList.push({
       recommendedTitel: '热门话题',
       recommendedCode: 'TJ002',
       rkv: specialTopicsList
     })
     recommendedList.push({
       recommendedTitel: '家庭类型',
       recommendedCode: 'TJ003',
       rkv: familyList
     })
     this.setData({
      tagTitles: recommendedList
     })
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setupInit()
    this.init()
  },

  setupInit: function () {
    utils.getFrameById('#navigationBar', (frame) => {
      // console.log('height:', frame.height)
      if (frame && frame.height) {
        this.setData({
          navHeight: frame.height
        })
      }
    })
  },

  tagItemClick:function(e){
    let sectionIndex = e.currentTarget.dataset.sectionindex
    let tagindex = e.currentTarget.dataset.tagindex
    this.data.tagTitles[sectionIndex].rkv[tagindex].selected = !this.data.tagTitles[sectionIndex].rkv[tagindex].selected
    this.setData({
      tagTitles: this.data.tagTitles,
      tagsList: JSON.parse(JSON.stringify(this.data.tagTitles))
    })
  },

  skipStep:function(){
    // utils.$http(app.globalData.api.SaveRecommended,{}, 'PUT', true, 'JSON').then(res => {
      wx.setStorageSync('showGuide', false)
      this.gotoMainPage()
    // })
  },

  chooseOkBtnClick:function(){
    let loop = this.data.tagsList.length
    this.data.tagsList.forEach((item,index) => {
      item.rkv = item.rkv.filter((item1, index) => {
        return item1.selected === true
      })
    })
    this.data.tagsList = this.data.tagsList.filter((item, index) => {
      return item.rkv.length > 0
    })

    let styleIdList = []
    let specialIdList = []
    let familyIdList = []
    this.data.tagsList.forEach(item => {
      if(item.recommendedCode === 'TJ001') {
        styleIdList = item.rkv
      } else if(item.recommendedCode === 'TJ002') {
        specialIdList = item.rkv
      } else  {
        familyIdList = item.rkv
      }
    })
    const styleId = styleIdList.map(item => item.id)
    const specialId = specialIdList.map(item => item.id)
    const familyId = familyIdList.map(item => item.id)
    
    const post = {
      id: null,
      openId: wx.getStorageSync('openId'),
      styleId: styleId.join(','),
      specialId: specialId.join(','),
      familyId: familyId.join(','),
    }

    utils.$http(app.globalData.api.SaveRecommended , post, 'PUT', false, 'JSON').then(res => {
      wx.setStorageSync('showGuide', false)
      this.gotoMainPage()
    })
  },

  gotoMainPage:function(){
    wx.switchTab({
      url: '/pages/home/main/index'
    })
    // let query = app.globalData.query
    // let _url = '' 
    // if (query == undefined) {
    //   _url = '/' + app.globalData.authToPage
    // } else {
    //   _url = '/' + app.globalData.authToPage + '?scene=' + query
    // }
    // wx.switchTab({
    //   url: _url
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})